﻿using System;
using System.Collections.Generic;
using com.huigo.Framework.ServiceUtils;
using com.loto.Common;
using System.Web.UI.WebControls;

namespace com.huigo.Admin.Web.customer
{
    public partial class AddCust : BasePage
    {
        private bool isModify
        {
            get
            {
                return cid == long.MinValue ? flgModify.Text == "0" ? false : true : true;
            }
        }

        public long cid
        {
            get
            {
                if (string.IsNullOrEmpty(Request.QueryString["cid"]))
                {
                    return long.MinValue;
                }
                else
                {
                    long c = long.MinValue;
                    if (long.TryParse(Request.QueryString["cid"], out c))
                        flgModify.Text = "1";
                    return c;
                }
            }
        }
        public string isLog = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();
                if (isModify)
                {
                    isLog = "<a href=\"javascript:GetLoginLogHistory('" + cid + "');\">查看登录日志</a>";
                    LoadData();
                }
            }
        }        

        private void BindList()
        {
            //销售员列表
            List<RequestFuncParams> argList = new List<RequestFuncParams>();            
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_USER_DROPLIST, argList);

            List<DropList> userDropList = Tools.ParserToDropList(parser, "UserId", "UserRealName");
            if (userDropList != null)
            {
                userList.DataSource = userDropList;
                userList.DataValueField = "Id";
                userList.DataTextField = "Value";
                userList.DataBind();
            }

            //来源列表
            List<DropList> sourceDropList = Tools.DictToRadioButtonList(Dict.DictCustSource);
            if (sourceDropList != null)
            {
                sourceList.DataSource = sourceDropList;
                sourceList.DataValueField = "Id";
                sourceList.DataTextField = "Value";
                sourceList.DataBind();
            }

            //类别列表
           // parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_CUST_TYPE_DROPLIST, argList);

            List<DropList> typeDropList = Tools.DictToDropList(Dict.DictCustInfoType);
            if (typeDropList != null)
            {
                typeList.DataSource = typeDropList;
                typeList.DataValueField = "Id";
                typeList.DataTextField = "Value";
                typeList.DataBind();
            }

            //等级列表
            parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_CUST_LEVEL_DROPLIST, argList);

            List<DropList> levelDropList = Tools.ParserToDropList(parser, "LevelId", "LevelName");
            if (levelDropList != null)
            {
                levelList.DataSource = levelDropList;
                levelList.DataValueField = "Id";
                levelList.DataTextField = "Value";
                levelList.DataBind();
            }

            //状态列表
            List<DropList> statusDropList = Tools.DictToRadioButtonList(Dict.DictCustStatus);
            if (statusDropList != null)
            {
                statusList.DataSource = statusDropList;
                statusList.DataValueField = "Id";
                statusList.DataTextField = "Value";
                statusList.DataBind();
            }

            //了解途径列表
            List<DropList> fromDropList = Tools.DictToDropList(Dict.DictCustFrom);
            if (fromDropList != null)
            {
                fromList.DataSource = fromDropList;
                fromList.DataValueField = "Id";
                fromList.DataTextField = "Value";
                fromList.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            List<RequestFuncParams> aList = new List<RequestFuncParams>();
            if (!isModify)
            {
                if (!VerifyField(FieldType.STRING, custName.Text, true))
                {
                    MessageBox.Show(this.Page, "用户名不能为空！");
                    return;
                }
                if (!VerifyField(FieldType.STRING, custPwd.Text, true))
                {
                    MessageBox.Show(this.Page, "密码不能为空！");
                    return;
                }
                argList.Add(new RequestFuncParams("custName", custName.Text));
                argList.Add(new RequestFuncParams("custPwd", new com.loto.Common.ASPMD5().MD5(custPwd.Text)));
            }
            else
            {
                argList.Add(new RequestFuncParams("custPwd", custPwd.Text == string.Empty ? string.Empty : new com.loto.Common.ASPMD5().MD5(custPwd.Text)));
            }

            if (!VerifyField(FieldType.STRING, custEmail.Text, true))
            {
                MessageBox.Show(this.Page, "EMAIL不能为空！");
                return;
            }
            if (!VerifyField(FieldType.STRING, sourceList.SelectedValue, true))
            {
                MessageBox.Show(this.Page, "请选择来源！");
                return;
            }
            if (!VerifyField(FieldType.STRING, statusList.SelectedValue, true))
            {
                MessageBox.Show(this.Page, "请选择状态！");
                return;
            }

            if (!VerifyField(FieldType.STRING, extContactName.Text, true))
            {
                MessageBox.Show(this.Page, "联系人姓名不能为空！");
                return;
            }
            if (!VerifyField(FieldType.STRING, extContactTel.Text, true))
            {
                MessageBox.Show(this.Page, "联系人电话不能为空！");
                return;
            }
            string[] data = (string[])Session["other"];
            if (data.Length == 13)
            {
                bool isExitst = false;

                if (data[0] != extContactName.Text)
                {
                    isExitst = true;
                }
                else if (data[1] != extContactTel.Text)
                {
                    isExitst = true;
                }
                else if (data[2] != extContactOthers.Text)
                {
                    isExitst = true;
                }
                else if (data[3] != extCompanyName.Text)
                {
                    isExitst = true;
                }
                else if (data[4] != extCompanyZip.Text)
                {
                    isExitst = true;
                }
                else if (data[5] != extCompanyAddress.Text)
                {
                    isExitst = true;
                }
                else if (data[6] != extCompanyTel.Text)
                {
                    isExitst = true;
                }
                else if (data[7] != extCompanyFax.Text)
                {
                    isExitst = true;
                }
                else if (data[8] != extCompanyEmail.Text)
                {
                    isExitst = true;
                }
                else if (data[9] != extCompanyWebsite.Text)
                {
                    isExitst = true;
                }
                else if (data[10] != extCompanyReg.Text)
                {
                    isExitst = true;
                }
                else if (data[11] != extChargeName.Text)
                {
                    isExitst = true;
                }
                else if (data[12] != fromList.SelectedItem.Text)
                {
                    isExitst = true;
                }
                if (isExitst)
                {
                    List<RequestFuncParams> requestList = new List<RequestFuncParams>();
                    requestList.Add(new RequestFuncParams("custFid", cid.ToString()));
                    requestList.Add(new RequestFuncParams("extContactName", data[0]));
                    requestList.Add(new RequestFuncParams("extContactTel", data[1]));
                    requestList.Add(new RequestFuncParams("extContactOthers", data[2]));
                    requestList.Add(new RequestFuncParams("extCompanyName", data[3]));
                    requestList.Add(new RequestFuncParams("extCompanyZip", data[4]));
                    requestList.Add(new RequestFuncParams("extCompanyAddress", data[5]));
                    requestList.Add(new RequestFuncParams("extCompanyTel", data[6]));
                    requestList.Add(new RequestFuncParams("extCompanyFax", data[7]));
                    requestList.Add(new RequestFuncParams("extCompanyEmail", data[8]));
                    requestList.Add(new RequestFuncParams("extCompanyWebsite", data[9]));
                    requestList.Add(new RequestFuncParams("extCompanyReg", data[10]));
                    requestList.Add(new RequestFuncParams("extChargeName", data[11]));
                    requestList.Add(new RequestFuncParams("extFrom", data[12]));
                    requestList.Add(new RequestFuncParams("userId", UserId.ToString()));
                    BaseService BaseService = new BaseService();
                    ParserXml ParserXml = BaseService.RequestEntryService(Dict.ERequestFunc.ADD_CUST_INFO_BACK, requestList);
                }
            }

            argList.Add(new RequestFuncParams("custEmail", custEmail.Text));
            argList.Add(new RequestFuncParams("custUserFid", userList.SelectedValue == string.Empty ? long.MinValue.ToString() : userList.SelectedValue));
            argList.Add(new RequestFuncParams("custFrom", sourceList.SelectedValue == string.Empty ? "0" : sourceList.SelectedValue));
            argList.Add(new RequestFuncParams("custTypeFid", typeList.SelectedValue == string.Empty ? "0" : typeList.SelectedValue));
            argList.Add(new RequestFuncParams("custLevelFid", levelList.SelectedValue == string.Empty ? "0" : levelList.SelectedValue));
            argList.Add(new RequestFuncParams("custStatus", statusList.SelectedValue==string.Empty?"0":statusList.SelectedValue));
            argList.Add(new RequestFuncParams("custMark", custMark.Text));

            argList.Add(new RequestFuncParams("extContactName", extContactName.Text));
            argList.Add(new RequestFuncParams("extContactTel", extContactTel.Text));
            argList.Add(new RequestFuncParams("extContactOthers", extContactOthers.Text));
            argList.Add(new RequestFuncParams("extCompanyName", extCompanyName.Text));
            argList.Add(new RequestFuncParams("extCompanyZip", extCompanyZip.Text));
            argList.Add(new RequestFuncParams("extCompanyAddress", extCompanyAddress.Text));
            argList.Add(new RequestFuncParams("extCompanyTel", extCompanyTel.Text));
            argList.Add(new RequestFuncParams("extCompanyFax", extCompanyFax.Text));
            argList.Add(new RequestFuncParams("extCompanyEmail", extCompanyEmail.Text));
            argList.Add(new RequestFuncParams("extCompanyWebsite", extCompanyWebsite.Text));
            argList.Add(new RequestFuncParams("extCompanyReg", extCompanyReg.Text));
            argList.Add(new RequestFuncParams("extChargeName", extChargeName.Text));
            argList.Add(new RequestFuncParams("extFrom", fromList.SelectedItem.Text));
            BaseService baseService = new BaseService();
            ParserXml parser;
            aList.Add(new RequestFuncParams("custName", custName.Text));
            aList.Add(new RequestFuncParams("email", custEmail.Text));

            if (isModify)
            {
                //是否存在
                aList.Add(new RequestFuncParams("cid", cid.ToString()));
                parser = baseService.RequestEntryService(Dict.ERequestFunc.CUST_IS_EXISTED, aList);
                if (parser != null && parser.RowsCount > 0 && parser["returnargs", 0] == "true")
                {
                    MessageBox.Show(this.Page, "用户名和邮箱已存在！");
                    return;
                }
                else
                {
                    argList.Add(new RequestFuncParams("cid", cid.ToString()));
                    parser = baseService.RequestEntryService(Dict.ERequestFunc.UPDATE_CUST_INFO, argList);
                }
            }
            else
            {
                aList.Add(new RequestFuncParams("cid", long.MinValue.ToString()));
                parser = baseService.RequestEntryService(Dict.ERequestFunc.CUST_IS_EXISTED, aList);
                if (parser != null && parser.RowsCount > 0 && parser["returnargs", 0] == "true")
                {
                    MessageBox.Show(this.Page, "用户名和邮箱已存在！");
                    return;
                }
                else
                {
                    parser = baseService.RequestEntryService(Dict.ERequestFunc.ADD_CUST, argList);
                }
            }
            //插入销售员关联表
            string type = string.Empty;
            string userId = userList.SelectedValue;
            if (!string.IsNullOrEmpty(userId))
            {
                if (isModify)
                {
                    type = "2";
                }
                else
                {
                    type = "1";
                }
                List<RequestFuncParams> list = new List<RequestFuncParams>();
                list.Add(new RequestFuncParams("custId", cid.ToString()));
                list.Add(new RequestFuncParams("userId", userId.ToString()));
                list.Add(new RequestFuncParams("type", type));
                list.Add(new RequestFuncParams("UserId", UserId.ToString()));
                BaseService service = new BaseService();
                ParserXml parserXml = service.RequestEntryService(Dict.ERequestFunc.ADD_SALES_RELATION, list);
            }

            if (parser != null)
                GoTo(GoToType.ADDSUC);
        }

        private void LoadData()
        {
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("cid", cid.ToString()));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_CUST_BY_ID, argList);
            ParserXml parser1 = baseService.RequestEntryService(Dict.ERequestFunc.GET_CUSTEXT_BY_ID, argList);            
            if (parser != null && parser.RowsCount > 0 && parser1 != null && parser1.RowsCount > 0)
            {
                custName.Text = parser["CustName", 0];
                custEmail.Text = parser["CustEmail", 0];
                if (userList.Items.FindByText(parser["UserRealName", 0]) != null)
                {
                    userList.Items.FindByText(parser["UserRealName", 0]).Selected = true;
                }
                
                //userList.SelectedValue = parser["UserRealName", 0];
                sourceList.SelectedValue = parser["CustFrom", 0];
                typeList.Items.FindByText(parser["TypeName", 0]).Selected = true;
                levelList.Items.FindByText(parser["LevelName", 0]).Selected = true;

                //typeList.SelectedItem.Text = parser["TypeName", 0];
                //levelList.SelectedItem.Text = parser["LevelName", 0];
                statusList.SelectedValue = parser["CustStatus", 0];
                custMark.Text = parser["CustMark", 0];

                custName.Enabled = false;

                //其他信息
                extContactName.Text = parser1["ExtContactName", 0];
                extContactTel.Text = parser1["ExtContactTel", 0];
                extContactOthers.Text = parser1["ExtContactOthers", 0];
                extCompanyName.Text = parser1["ExtCompanyName", 0];
                extCompanyZip.Text = parser1["ExtCompanyZip", 0];
                extCompanyAddress.Text = parser1["ExtCompanyAddress", 0];
                extCompanyTel.Text = parser1["ExtCompanyTel", 0];
                extCompanyFax.Text = parser1["ExtCompanyFax", 0];
                extCompanyEmail.Text = parser1["ExtCompanyEmail", 0];
                extCompanyWebsite.Text = parser1["ExtCompanyWebsite", 0];
                extCompanyReg.Text = parser1["ExtCompanyReg", 0];
                extChargeName.Text = parser1["ExtChargeName", 0];
                //fromList.SelectedItem.Text = parser1["ExtFrom", 0];
                fromList.Items.FindByText(parser1["ExtFrom", 0]).Selected = true;

                string[] other = new string[] { parser1["ExtContactName", 0], parser1["ExtContactTel", 0], parser1["ExtContactOthers", 0], parser1["ExtCompanyName", 0], parser1["ExtCompanyZip", 0], parser1["ExtCompanyAddress", 0], parser1["ExtCompanyTel", 0], parser1["ExtCompanyFax", 0], parser1["ExtCompanyEmail", 0], parser1["ExtCompanyWebsite", 0], parser1["ExtCompanyReg", 0], parser1["ExtChargeName", 0], parser1["ExtFrom", 0] };
                Session["other"] = other;
            }

        }
    }
}

